package presentacion;

import java.awt.Color;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;

import java.awt.BorderLayout;

import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.JButton;

import dominio.Usuario;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.text.ParseException;

import javax.swing.JFormattedTextField;
import javax.swing.text.MaskFormatter;

public class Login {

	private JFrame VentanaLogin;
	private JPanel pnlLogin;
	private JLabel lblDni;
	private JLabel lblContrasea;
	private JPasswordField psswContra;
	private JButton btnIdentificarse;
	private JFormattedTextField txtDni;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					Login window = new Login();
					window.VentanaLogin.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 * @throws java.text.ParseException 
	 */
	public Login() throws java.text.ParseException {
		initialize();
	}

	/**
	 * Initialize the contents of the frame.
	 * @throws java.text.ParseException 
	 */
	private void initialize() throws java.text.ParseException {
		VentanaLogin = new JFrame();
		VentanaLogin.setResizable(false);
		VentanaLogin.setTitle("Identif\u00EDcate");
		VentanaLogin.setBounds(100, 100, 285, 221);
		VentanaLogin.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		{
			pnlLogin = new JPanel();
			VentanaLogin.getContentPane().add(pnlLogin, BorderLayout.CENTER);
			pnlLogin.setLayout(null);
			{
				lblDni = new JLabel("DNI:");
				lblDni.setBounds(70, 13, 56, 16);
				pnlLogin.add(lblDni);
			}
			{
				lblContrasea = new JLabel("Contrase\u00F1a");
				lblContrasea.setBounds(70, 67, 90, 16);
				pnlLogin.add(lblContrasea);
			}
			{
				psswContra = new JPasswordField();
				psswContra.setBounds(70, 91, 116, 22);
				psswContra.addFocusListener(new MiFocusListener());
				pnlLogin.add(psswContra);
			}
			{
				btnIdentificarse = new JButton("Identificarse");
				btnIdentificarse.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent arg0) {
						try {
							Usuario u=new Usuario(null,null,txtDni.getText(),null,null);
							u.leerUsuario();
							if(u.getContrasena().equals(psswContra.getText())){
								Menu m = new Menu();
								m.VentanaMenu.setVisible(true);
								VentanaLogin.setVisible(false);	
							}else{
								JOptionPane.showMessageDialog(VentanaLogin,"Contrase�a erronea.");
							}
						} catch (Exception e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
							JOptionPane.showMessageDialog(VentanaLogin,"Usuario no encontrado.");
							
							
						}
					}
				});
				btnIdentificarse.setBounds(98, 126, 116, 25);
				pnlLogin.add(btnIdentificarse);
			}
			{
				MaskFormatter formatoDNI;
				try {
					formatoDNI = new MaskFormatter("########'-U");
					formatoDNI.setPlaceholderCharacter('X');
					txtDni = new JFormattedTextField(formatoDNI);
				} catch (ParseException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				txtDni.setBounds(70, 36, 90, 20);
				txtDni.addFocusListener(new MiFocusListener());
				pnlLogin.add(txtDni);
			}
		}
	}
	private class MiFocusListener extends FocusAdapter {
		@Override
		public void focusGained(FocusEvent e) {
			e.getComponent().setBackground(new Color(250,250,210));
		}
		@Override
		public void focusLost(FocusEvent e) {
			e.getComponent().setBackground(new Color(250,250,250));
		}
		}
}
